﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MpoDemo
{
    using System.ComponentModel;
    using System.Diagnostics;
    using System.IO;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public ImageSource Image0 { get; set; }
        public ImageSource Image1 { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            this.Loaded += this.MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.Load(@"Images\DSCF0090.MPO");
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            var data = e.Data as DataObject;
            if (data != null && data.ContainsFileDropList())
            {
                foreach (var path in data.GetFileDropList())
                {
                    this.Load(path);
                }
            }
        }

        private void Load(string path)
        {
            try
            {
                this.Cursor = Cursors.Wait;
                var images = MpoParser.GetImageSources(path).ToList();
                if (images.Count < 2) return;
                Image0 = images[0];
                Image1 = images[1];
                RaisePropertyChanged("Image0");
                RaisePropertyChanged("Image1");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
            finally
            {
                this.Cursor = Cursors.Arrow;
            }
        }

        #region PropertyChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string property)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }


}
